﻿using System;
using System.Collections.Generic;
using System.Text;

 
using System.Drawing ;

using System.Drawing.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.IO;
 
using DotSpatial.Data;
using DotSpatial.Data.Forms;

using DotSpatial.Symbology.Forms;

using DotSpatial.Topology;
using DotSpatial.Serialization;
using DotSpatial.Symbology;
 

namespace HaGisPack
{




    [Serializable ]
    public class FEPolygonLayer : FShapeLayer
    {
        public FEPolygonLayer(string id)
            : base(id)
        {
            this.m_Type = FeatureType.Polygon;

            this.Configure();


        }

        protected override void BuildLayer()
        {
            base.BuildLayer();
            //this._PolySymbol = (this.ILayer as IFeatureLayer).Symbolizer;
            (this.ILayer as IFeatureLayer).Symbolizer = this._PolySymbol as PolygonSymbolizer;
        }


        #region 属性

        [NonSerialized]
        private object _PolySymbol = new PolygonSymbolizer(Color.Red, Color.Red, 3);

        [Editor(typeof(PolygonSymbolEditor), typeof(UITypeEditor))]
        [Category("基体信息"), DisplayName("面基本信息设置")]
        public object PolygonSymbol
        {
            get { return _PolySymbol; }
            set 
            {
                this._PolySymbol = value;
                this.OnSetSymbol();
            }

        }

        protected void OnSetSymbol()
        {
            if (this.BRunning)
                (this.ILayer as IFeatureLayer).Symbolizer = (PolygonSymbolizer)this._PolySymbol;        
        }

        //-------------------------------------------------------------------------------------------//
        

        // 因为改变了选择机制，所以选择的图形要另写一套Category、Symbol了
        //public PolygonCategory pc = new PolygonCategory(Color.Yellow, Color.Blue, 3);

        [NonSerialized]
        private object _SelectSymbol = new PolygonSymbolizer();

        [Editor(typeof(PolygonSymbolEditor), typeof(UITypeEditor))]
        [Category("基体信息"), DisplayName("所选图形信息设置")]
        public object SelectionSymbol
        {
            get 
            {
                this._SelectSymbol = (this.ILayer as IPolygonLayer).SelectionSymbolizer;
                return _SelectSymbol as PolygonSymbolizer;
            }
            set
            {
                this._SelectSymbol = value;
                this.OnSetSelectSymbol();
            }
        }

        protected void OnSetSelectSymbol()
        {
            if (this.BRunning)
            {
                (this.ILayer as IFeatureLayer).SelectionSymbolizer = (PolygonSymbolizer)this._SelectSymbol;        

            }

        }




        #endregion



        # region protected methods

        public override void SaveEntity()
        {
            base.SaveEntity();


            XmlSerializer xs = new XmlSerializer();
            //string xmlLayer = xs.Serialize(this.ILayer);
            //File.WriteAllText(this.m_ParenLManager.layerPath + this.Id + "Layer.xml", xmlLayer);

            string xmlSymbol = xs.Serialize(this._PolySymbol);
            File.WriteAllText(this.m_ParenLManager.layerPath + this.Id + "Symbol.xml", xmlSymbol);

            string xmlSelectSymbol = xs.Serialize(this._SelectSymbol);
            File.WriteAllText(this.m_ParenLManager.layerPath + this.Id + "SelectSymbol.xml", xmlSelectSymbol);


            //string xmlLabel = xs.Serialize(this._LabelLayer);
            //File.WriteAllText(this.m_ParenLManager.layerPath + this.Id + "Label.xml", xmlLabel);

        }

        public override void LoadEntity()
        {
            base.LoadEntity();

            XmlDeserializer xd = new XmlDeserializer();

            //string xmlLayer = File.ReadAllText(this.m_ParenLManager.layerPath + this.Id + "Layer.xml");
            //xd.Deserialize(this.ILayer, xmlLayer);
            //this.ILayer = new DotSpatial.Controls.MapPolygonLayer();
            //this.ILayer = xd.Deserialize<DotSpatial.Controls.MapPolygonLayer>(xmlLayer);

            string xmlSymbol = File.ReadAllText(this.m_ParenLManager.layerPath + this.Id + "Symbol.xml");
            //this._PolySymbol = new PolygonSymbolizer();
            this._PolySymbol = xd.Deserialize<PolygonSymbolizer>(xmlSymbol);

            string xmlSelectSymbol = File.ReadAllText(this.m_ParenLManager.layerPath + this.Id + "SelectSymbol.xml");
            //this._SelectSymbol = new PolygonSymbolizer();
            this._SelectSymbol = xd.Deserialize<PolygonSymbolizer>(xmlSelectSymbol);

            //string xmlLabel = File.ReadAllText(this.m_ParenLManager.layerPath + this.Id + "Label.xml");
            //xd.Deserialize(this._LabelLayer, xmlSymbol);

        }



        # endregion

    }
}
